package com.hzy.quetosys.controller;

import com.github.pagehelper.PageInfo;
import com.hzy.quetosys.entity.Customer;
import com.hzy.quetosys.service.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class CustomerController {
private static final Logger logger= LoggerFactory.getLogger(CustomerController.class);
    @Autowired
    private CustomerService customerService;
    @GetMapping("customer/customer_add")
    public ModelAndView customerAddView(){
        ModelAndView mav=new ModelAndView("customer/customer_add");
        return mav;
    }
    @GetMapping("customer/customer_list")
    public ModelAndView customerListView(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "keyword",required = false) String keyword
    ){
        ModelAndView mav=new ModelAndView("customer/customer_list");

        PageInfo<Customer> page=customerService.findByKeyword(pageNum,pageSize,keyword);
        mav.addObject("page",page);
//        mav.addObject(page);
        return mav;
    }
    @GetMapping("/customer/customer_modify")
    public ModelAndView customerModifyView(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "id" ,required = true) int id,
            @RequestParam(name = "keyword",required = false) String keyword
    ){
        ModelAndView mav=new ModelAndView("customer/customer_modify");
        Customer customer=customerService.findById(id);
        mav.addObject("customer",customer);
        return mav;
    }
    @PostMapping("/customer/customer_add")
    public ModelAndView customerAdd(@Validated Customer customer, BindingResult bindingResult){

        ModelAndView mav=new ModelAndView("redirect:/customer/customer_add");
        Map<String,String> errorMessages=new HashMap<>();
        if(bindingResult.hasErrors()){
           final List<FieldError> fieldErrors = bindingResult.getFieldErrors();
           for(FieldError error:fieldErrors){
               errorMessages.put(error.getField(),error.getDefaultMessage());
           }
           mav.addObject("errorMessages",errorMessages);
           mav.addObject("customer",customer);
           mav.setViewName("customer/customer_add");
        }else {
           boolean result= customerService.append(customer);
           if(result){
               logger.info("新增客户信息成功-"+customer);
           }else {
               logger.info("新增客户信息失败-"+customer);
           }
        }

        return mav;
    }
    @PostMapping("/customer/customer_modify")
    public ModelAndView customerModify(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "keyword",required = false) String keyword,
            Customer customer
    ) throws UnsupportedEncodingException {
        String redirectUrl="redirect:/customer/customer_list?pageNum="+pageNum+"&pageSize="+pageSize+"&keyword="+ URLEncoder.encode(keyword,"utf-8");
        ModelAndView mav=new ModelAndView(redirectUrl);
        boolean result=customerService.modify(customer);
        return mav;
    }
@GetMapping("/customer/customer_remove")
    public ModelAndView customerRemove(
        @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
        @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
        @RequestParam(name = "id" ,required = true) int id,
        @RequestParam(name = "keyword",required = false) String keyword
) throws UnsupportedEncodingException {
    String redirectUrl="redirect:/customer/customer_list?pageNum="+pageNum+"&pageSize="+pageSize+"&keyword="+ URLEncoder.encode(keyword,"utf-8");
    ModelAndView mav=new ModelAndView(redirectUrl);
    boolean result=customerService.remove(id);
    return mav;
}
}
